package com.sky.service;

import java.sql.SQLException;
import java.util.List;

import com.sky.dao.UserDao;
import com.sky.domain.User;
import com.sky.exception.UserException;
import com.sky.utils.SendJMail;

public class UserService {
	
	UserDao userDao =new UserDao();
	//注册
    public  boolean    registerUser(User user) throws SQLException{
    	//调用dao层的方法进行注册(进行各种判断，该邮箱是否注册过，或者该用户是否注册过。。。。。。。。。)
    	
       //最新分析：插入数据的同时给邮箱发送激活码：
    	boolean isFlag = userDao.insertUserToDB(user);
    	//给邮箱发送激活码让用户去激活   激活(其实就是修改服务器的用户状态state)
    	System.out.println("==registerUser=="+user.getActiveCode());
    	SendJMail.sendMail(user.getEmail(), "欢迎注册本网站，请去激活帐号再去登录 若链接失效请复制链接到地址栏访问<a "
    			+ "href='http://localhost:8080/Demo01/activeCodeServlet?activeCode="+user.getActiveCode()+"'>http://localhost:8080/Demo01/activeCodeServlet?activeCode="+user.getActiveCode()+"</a>");
    	
    	return  isFlag;
    }
    
    //激活邮箱验证码
    public boolean activeCode(String code) throws SQLException{
    	 return   userDao.updateActiveCodeState(code);
    }
    
    //查询邮箱是否存在
    public String checkEmail(String email) throws SQLException{
    	//根据指定邮箱查找用户，如果用户存在，说明邮箱已经被注册过了，
    	User user = userDao.queryUserByEmail(email);
         if(user!=null){
        	 return  "邮箱已经被注册了";
         }
         return "邮箱可以正常使用";   
    }
	//登录
    public  User  LoginUser(User user) throws SQLException,UserException{
    	//1.登录之前先判断该用户是否存在
    	User user2 = userDao.queryUserByUserNameAndPwd(user);
    	
    	if(user2!=null){
    		//判断该用户是否被激活
    		if(user2.getState()==0){
    			//表示该用户未激活
    			throw new UserException("用户没有激活，请激活后再登录！");
    		}
    	}else{
    		throw new UserException("用户不存在！");
    	}
    	
    	return user2;
    }
    //根据用户名查询密码
    public  String  queryPwdByUserName(String name)throws SQLException{
    	return  userDao.queryPwdbyUserName(name);
    }

    //根据用户信息(用户名)修改密码
	public void  updatePwd(User user)throws SQLException,UserException {
		// TODO Auto-generated method stub
		int rowCount=   userDao.updatePwd(user);
		if(rowCount>0){
			
		}else{
			throw new UserException("修改密码失败！");
		}
		
	}

	public List<User> queryUserList() throws SQLException{
		// TODO Auto-generated method stub
		return userDao.queryUserList();
	}

	public void deleteUserById(String id) throws SQLException, UserException {
		// TODO Auto-generated method stub
		int rowCount = userDao.deleteUserById(id);
		if(rowCount>0){
			
		}else{
			throw new UserException("删除用户失败");
		}
	}

	public List<User> queryUserByKey(String keyString) throws SQLException {
		// TODO Auto-generated method stub
		return userDao.queryUserByKey(keyString);
	}
	//唐超
	public void updateuserinfo(User user) throws SQLException, UserException {
		// TODO Auto-generated method stub
//修改个人信息
		int rowCount = userDao.updateUserById(user);
		 if(rowCount>0){
			 
		 }else {
			throw new UserException("修改失败");
		}
	}
//根据ID查用户信息
public User queryUserById(String id) throws SQLException {
		// TODO Auto-generated method stub
		return userDao.queryUserById(id);
	}
	
	
	
}
